<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1,minimum-scale=1,maximum-scale=1">


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />



  <meta name="description" content="Nest连接数据库"/>




  <meta name="keywords" content="node," />





  <link rel="alternate" href="/default" title="刘粉丝来了">






<link rel="canonical" href="http://yoursite.com/2020/06/14/nestMongodb/"/>


<meta name="description" content="第一步：安装依赖 npm add nestjs-typegoose   npm add @typegoose&#x2F;typegoose mongoose @types&#x2F;mongoose  第二步：进入项目主模型app.module.ts里导入以下依赖以及配置12345678910111213import &amp;#123; TypegooseModule &amp;#125; from &#39;nestjs-typegoo">
<meta property="og:type" content="article">
<meta property="og:title" content="Nest连接数据库">
<meta property="og:url" content="http://yoursite.com/2020/06/14/nestMongodb/index.html">
<meta property="og:site_name" content="刘粉丝来了">
<meta property="og:description" content="第一步：安装依赖 npm add nestjs-typegoose   npm add @typegoose&#x2F;typegoose mongoose @types&#x2F;mongoose  第二步：进入项目主模型app.module.ts里导入以下依赖以及配置12345678910111213import &amp;#123; TypegooseModule &amp;#125; from &#39;nestjs-typegoo">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-06-14T13:33:12.000Z">
<meta property="article:modified_time" content="2020-06-14T16:33:55.009Z">
<meta property="article:author" content="刘粉丝来了">
<meta property="article:tag" content="node">
<meta name="twitter:card" content="summary">


<link rel="stylesheet" type="text/css" href="/css/style.css?v=1.1" />
<link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet'>





<script type="text/javascript">
  var themeConfig = {
    fancybox: {
      enable: false
    },
  };
</script>




  



    <title> Nest连接数据库 - 刘粉丝来了 </title>
  <meta name="generator" content="Hexo 4.2.0"></head>

  <body>
    <div id="page">
      <header id="masthead"><div class="site-header-inner">
    <h1 class="site-title">
        <a href="/." class="logo">刘粉丝来了</a>
    </h1>

    <nav id="nav-top">
        
            <ul id="menu-top" class="nav-top-items">
                
                    <li class="menu-item">
                        <a href="/">
                            
                            
                                Home
                            
                        </a>
                    </li>
                
                    <li class="menu-item">
                        <a href="/archives">
                            
                            
                                Archives
                            
                        </a>
                    </li>
                
                    <li class="menu-item">
                        <a href="/tags">
                            
                            
                                Tags
                            
                        </a>
                    </li>
                
                    <li class="menu-item">
                        <a href="/about">
                            
                            
                                About
                            
                        </a>
                    </li>
                
            </ul>
        
  </nav>
</div>

      </header>
      <div id="content">
        
    <div id="primary">
        
  <article class="post">
    <header class="post-header">
      <h1 class="post-title">
        
          Nest连接数据库
        
      </h1>

      <time class="post-time">
          6月 14 2020
      </time>
    </header>



    
            <div class="post-content">
            <h3 id="第一步：安装依赖"><a href="#第一步：安装依赖" class="headerlink" title="第一步：安装依赖"></a><strong>第一步：安装依赖</strong></h3><blockquote>
<p>npm add nestjs-typegoose</p>
</blockquote>
<blockquote>
<p>npm add @typegoose/typegoose mongoose @types/mongoose</p>
</blockquote>
<h3 id="第二步：进入项目主模型app-module-ts里导入以下依赖以及配置"><a href="#第二步：进入项目主模型app-module-ts里导入以下依赖以及配置" class="headerlink" title="第二步：进入项目主模型app.module.ts里导入以下依赖以及配置"></a><strong>第二步：进入项目主模型<code>app.module.ts</code>里导入以下依赖以及配置</strong></h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; TypegooseModule &#125; <span class="keyword">from</span> <span class="string">'nestjs-typegoose'</span></span><br><span class="line"></span><br><span class="line">@Module(&#123;</span><br><span class="line">  imports: [</span><br><span class="line">    <span class="comment">// 关键代码</span></span><br><span class="line">    TypegooseModule.forRoot(<span class="string">"mongodb://localhost:27017/nest-bolg-api"</span>,&#123;</span><br><span class="line">      useNewUrlParser:<span class="literal">true</span></span><br><span class="line">    &#125;),</span><br><span class="line">    PostsModule</span><br><span class="line">  ],</span><br><span class="line">  controllers: [AppController],</span><br><span class="line">  providers: [AppService],</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="第三步：进入需要配置的模型-例如我的模型是Posts然后进入posts-module-ts做如下配置"><a href="#第三步：进入需要配置的模型-例如我的模型是Posts然后进入posts-module-ts做如下配置" class="headerlink" title="第三步：进入需要配置的模型 例如我的模型是Posts然后进入posts.module.ts做如下配置"></a><strong>第三步：进入需要配置的模型 例如我的模型是<code>Posts</code>然后进入<code>posts.module.ts</code>做如下配置</strong></h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 导入设置的模型</span></span><br><span class="line"><span class="keyword">import</span> &#123; Post &#125; <span class="keyword">from</span> <span class="string">'./posts.model'</span></span><br><span class="line"></span><br><span class="line">@Module(&#123;</span><br><span class="line">  imports:[</span><br><span class="line">    TypegooseModule.forFeature([Post])</span><br><span class="line">  ],</span><br><span class="line">  controllers: [PostsController]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="第四步：进入Posts-controller-ts里做如下配置"><a href="#第四步：进入Posts-controller-ts里做如下配置" class="headerlink" title="第四步：进入Posts.controller.ts里做如下配置"></a><strong>第四步：进入<code>Posts.controller.ts</code>里做如下配置</strong></h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 导入模型 Post与qqi请求方法Post冲突所以用 as postSchema代替Post 取个别名</span></span><br><span class="line"><span class="keyword">import</span> &#123; Post <span class="keyword">as</span> postSchema &#125; <span class="keyword">from</span> <span class="string">'./posts.model'</span></span><br><span class="line"><span class="keyword">import</span> &#123; ModelType &#125; <span class="keyword">from</span> <span class="string">'@typegoose/typegoose/lib/types'</span></span><br><span class="line"></span><br><span class="line">@Controller(<span class="string">'posts'</span>)</span><br><span class="line"><span class="keyword">export</span> <span class="class"><span class="keyword">class</span> <span class="title">PostsController</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>(</span><br><span class="line">        // @InjectModel注入 private 修饰符  readonly 只读  ModuleType增加提示</span><br><span class="line">        @InjectModel(postSchema) private readonly postSchema:ModelType&lt;postSchema&gt;</span><br><span class="line">    )&#123;&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    @Get()</span><br><span class="line">    @ApiProperty(&#123;<span class="attr">summery</span>:<span class="string">'创建帖子'</span>&#125;)</span><br><span class="line">    create( @Body() body:string ) &#123;</span><br><span class="line">        <span class="comment">// 使用 @InjectModel后 访问模型必须以 `this.postSchema` 方法获取 一点定加上this</span></span><br><span class="line">        <span class="keyword">await</span> <span class="keyword">this</span>.postSchema.create( body )</span><br><span class="line">        <span class="keyword">return</span> &#123; <span class="attr">status</span>:<span class="number">200</span> &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

            </div>
          

    
      <footer class="post-footer">
		
		<div class="post-tags">
		  
			<a href="/tags/node/">node</a>
		  
		</div>
		

        
        
  <nav class="post-nav">
    
      <a class="prev" href="/2020/06/15/nestSwagger/">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">Nest配置Swagger接口文档以及Nest的基本操作</span>
        <span class="prev-text nav-mobile">上一篇</span>
      </a>
    
    
      <a class="next" href="/2020/06/11/mongodb/">
        <span class="next-text nav-default">mongodb</span>
        <span class="prev-text nav-mobile">下一篇</span>
        <i class="iconfont icon-right"></i>
      </a>
    
  </nav>

        
  <div class="comments" id="comments">
    
  </div>


      </footer>
    
  </article>


    </div>

      </div>

      <footer id="colophon"><script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
<span class="copyright-year" style="width:100%; display:flex; justify-content: space-between;">
    <span>
        
        &copy;
    
        2014 -
    
    2020
    <span class="footer-author">刘粉丝来了.</span>
    <span class="power-by">
        由 <a class="hexo-link" href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a> and <a class="theme-link" href="https://github.com/frostfan/hexo-theme-polarbear" target="_blank" rel="noopener">Polar Bear</a> 强力驱动
    </span>
    </span>
    <span id="busuanzi_container_site_pv">
        本站总访问量<span id="busuanzi_value_site_pv" style="color:red">24280</span>次
    </span>
</span>



      </footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div>
    


    




  
    <script type="text/javascript" src="/lib/jquery/jquery-3.1.1.min.js"></script>
  

  

    <script type="text/javascript" src="/js/src/theme.js?v=1.1"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=1.1"></script>


  </body>
</html>
